package com.wf.course.web.controller.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.goods.GoodsInfo;
import com.wf.course.model.goods.GoodsParam;
import com.wf.course.service.IGoodsInfoService;
import com.wf.course.service.IGoodsParamService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 列表Controller
 */
@RequestMapping("goodsParam")
@Controller
public class GoodsParamController {

	@Autowired
	private IGoodsInfoService goodsInfoService;
	@Autowired
	private IGoodsParamService goodsParamService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param goodsName
	 * @param paramName
	 * @param model
	 * @return
	 */
	@RequestMapping("goodsParamList")
	public String goodsInfoList(HttpSession httpSession,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "goodsName", required = false) String goodsName, 
			@RequestParam(value = "paramName", required = false) String paramName, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("goodsName", goodsName);
		cons.put("paramName", paramName);
		PageBean<GoodsParam> pageBean = new PageBean<GoodsParam>(pageNo, pageSize, null, cons);
		pageBean.setUrl("goodsParam/goodsParamList");
		this.goodsParamService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		httpSession.setAttribute("page", pageBean);
		
		return "goods/goodsParamList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model,HttpSession httpSession) {
		 
		if (id != null && id != 0) {
			GoodsParam goodsParam = goodsParamService.queryGoodsParamById(id);
			model.addAttribute("goodsParam", goodsParam);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 
		 List<GoodsInfo> goods = goodsInfoService.queryGoods();
		 model.addAttribute("goods",goods);
		 
		return "goods/goodsParamEdit";
	}

	/**
	 * 新增和修改
	 * @param goodsParam
	 * @param request
	 * @return
	 */
 	@LogInfo("修改或新增商品参数")
	@RequestMapping(value = "goodsParamEdit" , produces = "text/html;charset=UTF-8")
	public String goodsParamEdit(@ModelAttribute GoodsParam goodsParam,
			MultipartFile imgFile,
			HttpServletRequest request,HttpSession httpSession,Model model) {
		
		goodsParamService.saveOrUpdateGoodsParam(goodsParam,imgFile);
		
		//保存后，跳转到之前的列表页
		@SuppressWarnings("unchecked")
		PageBean<T> page =  (PageBean<T>) httpSession.getAttribute("page");
		 
		PageBean<GoodsParam> pageBean = new PageBean<GoodsParam>(page.getActulPageNo(), page.getPageSize(), null, page.getConMap());
		pageBean.setUrl("goodsParam/goodsParamList");
		this.goodsParamService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
	 		
		return "goods/goodsParamList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除商品参数")
	@RequestMapping(value = "remove/{id}")
	public String removeGoodsParam(@PathVariable("id") Integer id) {
		
		goodsParamService.deleteById(id);
		
		return "redirect:/goodsParam/goodsParamList";
	}
	

}
